package com.fbinggun.fattribute;

import com.fbinggun.fattribute.attribute.AttributeManage;
import com.fbinggun.fattribute.attribute.IAttribute;
import com.fbinggun.fattribute.attribute.attack.*;
import com.fbinggun.fattribute.attribute.defense.Defense;
import com.fbinggun.fattribute.attribute.defense.Dodge;
import com.fbinggun.fattribute.attribute.defense.PoisonDefense;
import com.fbinggun.fattribute.attribute.defense.WitherDefense;
import com.fbinggun.fattribute.attribute.other.*;
import com.fbinggun.fattribute.data.DataManage;
import com.fbinggun.fattribute.data.EntityData;
import com.fbinggun.fattribute.data.PlayerData;
import com.fbinggun.fattribute.listener.AttackListener;
import com.fbinggun.fattribute.listener.DefenseListener;
import com.fbinggun.fattribute.listener.EntityUpdateListener;
import com.fbinggun.fattribute.papi.AttributePAPI;
import com.fbinggun.fattribute.util.TempData;
import com.fbinggun.fbinggunutil.*;
import com.fbinggun.sql.SQLite;
import org.bukkit.scheduler.BukkitRunnable;
import java.sql.SQLException;
import java.util.*;

public final class FAttributePlugin extends FPlugin {
    public static   FAttributePlugin plugin;
    public static Set<IAttribute> attributeTypeMap = new HashSet<>();
    public static JsonTable tempTable;
    public static SQLite sql ;
    @Override
    public void onEnable() {
        plugin=this;
        reload();
        new AttributePAPI(this).register();
        // Plugin startup logic

    }

    @Override
    public void reload() {
        unListener();
        DataManage.setDataMap(new HashMap<>());
        attributeTypeMap= new HashSet<>();
        AttributeManage.attributeMap= new HashMap<>();
        saveDefaultConfig();
        reloadConfig();
        try {
            sql= SqlUtil.reloadSQL(this,getConfig().getConfigurationSection("SQL"));
            tempTable = new JsonTable(sql,getConfig().getString("SQL.table"));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        registerAttribute();
        getServer().getPluginManager().registerEvents(new AttackListener(),this);
        getServer().getPluginManager().registerEvents(new EntityUpdateListener(),this);
        getServer().getPluginManager().registerEvents(new DefenseListener(),this);

        new BukkitRunnable() {
            @Override
            public synchronized void run() {
                try {
                    Map<UUID,EntityData> map = new HashMap<>();
                    map.putAll(DataManage.getDataMap());
                    for(EntityData entityData:map.values()){
                        entityData.tick();
                    }
                }catch (Exception e){

                }

            }
        }.runTaskTimer(this,0,1);
    }

    @Override
    public FPlugin getPlugin() {
        return plugin;
    }

    public  Map<IAttribute,Double> getSaveEntityData(String uuid){
//        List<String> list = tempConfig.getStringList("EntityData."+uuid);
//        if(list!=null&&list.size()!=0){
//            return AttributUtil.getAttribut(list);
//        }
        return null;
    }
    public List<String> getSaveEntityDataList(EntityData entityData){
        Map<IAttribute,Double> data =entityData.statusMap.getAttributeMap();
        if(data!=null){
            List<String> list =new ArrayList<>();
            for(IAttribute attribute:data.keySet()){
                double v = data.get(attribute);
                list.add(attribute.getKey()+": "+v);
            }
            return list;
        }
        return null;

    }

    public void saveEntityData() throws SQLException, ClassNotFoundException {
        if(DataManage.getDataMap()!=null){
            for(EntityData data:DataManage.getDataMap().values()){
                if(data instanceof PlayerData){
                    continue;
                }
                TempData tempData = new TempData(data);
                tempTable.setJson(data.getUuid().toString(),tempData.toJson().toString());
            }
        }
    }
    @Override
    public void onDisable() {
        try {
            saveEntityData();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        // Plugin shutdown logic
    }

    public static Set<IAttribute> getAttributeTypeMap() {
        return attributeTypeMap;
    }

    public static void log(String key){
        plugin.getLogger().info(key);
    }

    public String getConfigKey(String key){
      return getConfig().getString("Attribute."+key);
    }
    public void registerAttribute(){
        AttributeManage.registerAttribute(new Attack());
        AttributeManage.registerAttribute(new Bleed());
        AttributeManage.registerAttribute(new BanMove());
        AttributeManage.registerAttribute(new Crit());
        AttributeManage.registerAttribute(new CritDefense());
        AttributeManage.registerAttribute(new CritRate());
        AttributeManage.registerAttribute(new Health());
        AttributeManage.registerAttribute(new Dodge());
        AttributeManage.registerAttribute(new Defense());
        AttributeManage.registerAttribute(new Hit());
        AttributeManage.registerAttribute(new Continuous());
        AttributeManage.registerAttribute(new AttackSpeed());
        AttributeManage.registerAttribute(new PoisonAttack());
        AttributeManage.registerAttribute(new PoisonDefense());
        AttributeManage.registerAttribute(new WitherDefense());
        AttributeManage.registerAttribute(new WitherAttack());
        AttributeManage.registerAttribute(new SlotDigging());
        AttributeManage.registerAttribute(new Frighten());
    }


}
